'use client';
import { useTranslations } from 'next-intl';
import { Table, theme } from 'antd';
import { useRouter } from 'next/navigation';
import AvaForm from './AvaForm';
import { Space, Tag, type TableProps } from 'antd';
import Layout from '@/components/Layout';
import styles from '../user/index.module.less';
import React, { useState } from 'react';
import { Button, Modal } from 'antd';
import Myform from '../../../../component/myform';
export default function User() {
  const t = useTranslations('user');
  const { token } = theme.useToken();
  const [isModalOpen, setIsModalOpen] = useState(false);
  let [title,settitle]=useState("")
  let [data1,setdata1]=useState()
  const showModal = () => {
    setIsModalOpen(true);
  };

  const handleOk = () => {
    setIsModalOpen(false);
  };

  const handleCancel = () => {
    setIsModalOpen(false);
  };
  const listStyle: React.CSSProperties = {
    background: token.colorFillAlter,
    borderRadius: token.borderRadiusLG,
    padding: 12
  };
  
interface DataType {
  key: string;
  name: string;
  role: number;
  desc: string;
  tags: string[];
}
let [data,setdata] = useState([
  {
    key: '1',
    name: '徐小夕',
    role: 1,
    desc: 'H5-Dooring作者, 掘金签约作者，知乎专栏作家',
    tags: '前端工程师', 
  },
  {
      key: '2',
      name: '老六',
      role: 2,
      desc: '知乎专栏作家',
      tags: '前端工程师'
  },
  {
      key: '3',
      name: '李盟',
      role: 2,
      desc: 'Dooring共建者',
      tags: '后端工程师'
  },
  {
      key: '4',
      name: '王阿明',
      role: 2,
      desc: '技术合伙人',
      tags: '全栈工程师'
  },
  {
      key: '5',
      name: '张小明',
      role: 2,
      desc: '技术合伙人',
      tags: '全栈工程师'
  },
  {
      key: '6',
      name: 'Tom',
      role: 2,
      desc: '技术合伙人',
      tags: '全栈工程师'
  },
  {
      key: '7',
      name: 'Json',
      role: 2,
      desc: '技术合伙人',
      tags: '全栈工程师'
  },
])
const columns: TableProps<DataType>['columns'] = [
  {
    title: '用户名',
    dataIndex: 'name',
    key: 'name',
    render: (text) => <a>{text}</a>,
    fixed: 'left',
    width: 100
  },
  {
    title: '角色',
    dataIndex: 'role',
    key: 'role',
    render: role => role === 1 ? '超级管理员' : '开发者'
  },
  {
    title: '简介',
    dataIndex: 'desc',
    key: 'desc',
  },
  {
    title: '标签',
    key: 'tags',
    dataIndex: 'tags',
    render: ((tags)=>{
   return   <Tag key={tags}>
      {tags}
    </Tag>
    })
  },
  {
    title: '操作',
    key: 'action',
    render: (_, record) => (
      <Space size="middle">
        <a onClick={()=>{
              showModal()
          settitle("编辑")
          setdata1(record)
        }}>编辑</a>
        <a onClick={()=>{
          let ddd=data.filter(item=>{
            return item.key!=record.key
          })
          setdata(ddd)
        }}>删除</a>
      </Space>
    ),
  },
];

function xiugai(obj){
  return setdata(data.map(item=>item.key==obj.key?obj:item))
}

  return (
    <div>
       
    <Layout curActive='/fff'>
      <main className={styles.userWrap}>
        <div className={styles.content}>
          <AvaForm />
          <div style={listStyle}>
            <h3>{t('userList')}</h3>
            <Button type="primary" onClick={()=>{
              showModal()
              settitle("添加")
              setdata1({})
}}>
       添加
      </Button>
      <Modal title={title} open={isModalOpen} onOk={handleOk} onCancel={handleCancel}>
        <Myform  title={title} onOk={handleOk} data1={data1} data={data} key={new Date().getTime()} xiugai={xiugai}></Myform>
      </Modal>
            <Table columns={columns} dataSource={data} pagination={{ pageSize: 5 }}scroll={{ x: 1000 }} />
          </div>
        </div>
      </main>
    </Layout>
</div>
  );
}
